package com.runa.monitor.platinterface.netservice.handler;

import com.runa.monitor.platinterface.netservice.server.Server;
import com.runa.monitor.platinterface.base.AnalysisFactory;
import com.runa.protocol.enums.ModbusType;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;

/**
 * @author zhuj
 */
public class DtuChannelInitializer extends ChannelInitializer {

    private DtuRegisterHandler registerHandler = new DtuRegisterHandler();

    private ResponseHandler responseHandler;

    private ModbusType modbusType;

    private String dtuProtocol;

    public DtuChannelInitializer(String dtuProtocol, ModbusType modbusType, Server server) {
        this.dtuProtocol = dtuProtocol;
        this.modbusType = modbusType;
        responseHandler = new ResponseHandler(server);
    }

    @Override
    protected void initChannel(Channel ch) {
        ch.pipeline().addLast(new ProtocolDecoder(AnalysisFactory.create(dtuProtocol, modbusType)));
        ch.pipeline().addLast(new ProtocolEncoder());
        ch.pipeline().addLast(registerHandler);
        ch.pipeline().addLast(responseHandler);
    }
}
